<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 流失客户
 *
 * @icon fa fa-circle-o
 */
class TransLostCustomer extends Backend
{

    /**
     * TransLostCustomer模型对象
     * @var \app\common\model\TransLostCustomer
     */
    protected $model = null;

    /**
     * user模型对象
     * @var \app\common\model\user
     */
    protected $userModel = null;

    /**
     * user模型对象
     * @var \app\common\model\user
     */
    protected $termModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\TransLostCustomer;
        $this->userModel = new \app\common\model\User;
        $this->termModel = new \app\common\model\Terminal;
        $this->view->assign("processList", $this->model->getProcessList());
        $this->view->assign("standsTypsList", $this->model->getStandsTypsList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        $parms = $this->request->param();

        $offset = $this->request->param("offset");
        $limit = $this->request->param("limit");


        $now =  strtotime(date("Y-m-d", time()));

        $where = "ends < " .  $now;

        $field = "*,";
        $field .= "UNIX_TIMESTAMP(FROM_UNIXTIME( last_time, '%Y-%m-%d' )) lasts,";
        $field .= "CAST( UNIX_TIMESTAMP( DATE_ADD( FROM_UNIXTIME( last_time, '%Y-%m-%d' ), INTERVAL termDays DAY ) ) AS UNSIGNED ) ends";
        $buildSql = Db::name("trans_lost_customer")->field($field)->buildSql();

        $total =  Db::table($buildSql)
            ->alias('a')
            ->where($where)
            ->count();

        $lists = Db::table($buildSql)
            ->alias('a')
            ->where($where)
            ->limit((int)$offset, (int) $limit)
            ->select();
        if ($total) {
            foreach ($lists as $k => $v) {
                $lists[$k]["name"]  = $this->userModel->where("id = " . $v['uid'])->value('nickname');
            }
        }
        $result = ['total' => $total, 'rows' => $lists];
        return json($result);
    }


    /**
     * 详情
     */
    public function details($ids)
    {
        $row = Db::name("trans_lost_customer")
            ->where("id", $ids)
            ->field("*,last_time,UNIX_TIMESTAMP(FROM_UNIXTIME( last_time, '%Y-%m-%d' )) lasts,CAST( UNIX_TIMESTAMP( DATE_ADD( FROM_UNIXTIME( last_time, '%Y-%m-%d' ), INTERVAL termDays DAY ) ) AS UNSIGNED ) ends")
            ->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $pro = Db::name('product')->column("name", "id");
        $trans = Db::name("terminal")
            ->where("user_id", $row["uid"])
            ->field("*,from_unixtime(binding_time , '%Y-%m-%d') binds")
            ->select();
        foreach ($trans as $key => $vals) {
            $trans[$key]["name"] = $pro[$vals["product_id"]];
            // $trans[$key]["typeNick"] = $vals["money"] >  $warnNums ?  '已超标' : "未超标";
        }
        $this->view->assign("lists", $trans);
        return $this->view->fetch();
    }
}
